﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SqlServer = DataCoreTechnologies.SqlServer;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal class TriggersDocument : Document
    {
        #region Public
        //Constructor
        internal TriggersDocument(SqlServer.Database database)
        {
            DocumentType = DocumentType.Triggers;
            RootDatabase = database;
            RootServer = RootDatabase.Server;
            URL = "index." + RootDatabase.Name + @".triggers.html";
            _database = database;
        }
        //Properties
        internal override string DisplayName { get { return "Triggers"; } }
        //Methods
        internal override string GetHtml()
        {
            if (_html == "")
            {

                AddCssLine(@"      	.nameColumn {width: 300px; word-wrap: break-word;}");
                AddCssLine(@"      	.parentColumn {width: 15%;}");

                AddBodyLine(@"<div id=""mainBody"">");

                string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name + @"</a> . ";
                heirarchy += @"<a href=""http://index." +
                    _database.Name + @".html"">" + ScrubHtml(_database.Name) + @"</a></h2>";
                AddBodyLine(heirarchy);
                AddBodyLine(@"<h2 id=""title"">" + ScrubHtml(_database.Name) + @" Triggers</h2>");

                #region Database Triggers
                var dbTriggers = (from trigger in _database.Triggers
                                orderby trigger.Name
                                select trigger);

                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_databaseTriggers"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('databaseTriggers');"">Database Triggers</a>");
                AddBodyLine(@"</h2>");
                
                AddBodyLine(@"<div id=""databaseTriggers"">");
                AddBodyLine(@"<table>");
                string codeLine = @"<colgroup><col class=""nameColumn"" />" +
                    @"<col class=""descriptionColumn"" /></colgroup>";
                AddBodyLine(codeLine);

                AddBodyLine(@"<tr><th>Name</th><th>Description</th></tr>");

                Boolean evenRow = true;
                StringBuilder sb = new StringBuilder();
                foreach (SqlServer.Trigger trigger in dbTriggers)
                {
                    if (evenRow == true)
                        sb.Append(@"<tr class=""evenRow"">");
                    else
                        sb.Append(@"<tr class=""oddRow"">");
                    evenRow = !evenRow;

                    string href = @"<td class=""name"">";
                    href += @"<a href=""http://index." + RootDatabase.Name
                        + @".trigger." + trigger.Name + @".html"">" + ScrubHtml(trigger.Name) + @"</a>";
                    href += @"</td>";
                    sb.Append(href);

                    if (trigger.Description == "")
                        sb.Append(@"<td>&nbsp;</td></tr>");
                    else
                        sb.Append(@"<td>" + ScrubHtml(trigger.Description) + @"</td></tr>");

                    AddBodyLine(sb.ToString());
                }
                
                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion
                #region Table Triggers
                var tableTriggers = (from schema in _database.Schemas
                                    from table in schema.Tables
                                    from trigger in table.Triggers
                                    orderby trigger.Name
                                    select trigger);

                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_tableTriggers"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('tableTriggers');"">Table Triggers</a>");
                AddBodyLine(@"</h2>");
                

                AddBodyLine(@"<div id=""tableTriggers"">");
                AddBodyLine(@"<table>");
                AddBodyLine(@"<colgroup><col class=""nameColumn"" /><col class=""parentColumn"" /><col class=""descriptionColumn"" /></colgroup>");
                AddBodyLine(@"<tr><th>Name</th><th>Table</th><th>Description</th></tr>");

                evenRow = true;
                sb = null;
                foreach (SqlServer.Trigger trigger in tableTriggers)
                {
                    sb = new StringBuilder();
                    if (evenRow == true)
                        sb.Append(@"<tr class=""evenRow"">");
                    else
                        sb.Append(@"<tr class=""oddRow"">");
                    evenRow = !evenRow;

                    string href = @"<td>";
                    href += @"<a href=""http://index." + RootDatabase.Name + "." +
                        trigger.Table.Schema.Name + "." + trigger.Table.Name + 
                        ".trigger." + trigger.Name + @".html"">" + ScrubHtml(trigger.Name) + @"</a>";
                    href += @"</td>";
                    sb.Append(href);

                    href = @"<td>";
                    if (Business.FilterPreferences.ShowTablesPage)
                    {
                       href += @"<a href=""http://index." + RootDatabase.Name + "." +
                           trigger.Table.Schema.Name + ".table." + trigger.Table.Name +
                           @".html"">" + ScrubHtml(trigger.Table.Schema.Name) + "." + ScrubHtml(trigger.Table.Name) + @"</a>";
                    }
                    else
                    {
                       href += ScrubHtml(trigger.Table.Schema.Name) + "." + ScrubHtml(trigger.Table.Name);
                    }
                    href += @"</td>";
                    sb.Append(href);

                    if (trigger.Description == "")
                        sb.Append(@"<td>&nbsp;</td></tr>");
                    else
                        sb.Append(@"<td>" + ScrubHtml(trigger.Description) + @"</td></tr>");

                    AddBodyLine(sb.ToString());
                }
                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion
                #region View Triggers
                var viewTriggers =  (from schema in _database.Schemas
                                     from view in schema.Views
                                     from trigger in view.Triggers
                                     orderby trigger.Name
                                     select trigger);
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_viewTriggers"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('viewTriggers');"">View Triggers</a>");
                AddBodyLine(@"</h2>");

                AddBodyLine(@"<div id=""viewTriggers"">");
                AddBodyLine(@"<table>");
                AddBodyLine(@"<colgroup><col class=""nameColumn"" /><col class=""parentColumn"" /><col class=""descriptionColumn"" /></colgroup>");
                AddBodyLine(@"<tr><th>Name</th><th>View</th><th>Description</th></tr>");

                evenRow = true;
                sb = new StringBuilder();
                foreach (SqlServer.Trigger trigger in viewTriggers)
                {
                    if (evenRow == true)
                        sb.Append(@"<tr class=""evenRow"">");
                    else
                        sb.Append(@"<tr class=""oddRow"">");
                    evenRow = !evenRow;

                    string href = @"<td class=""name"">";
                    href += @"<a href=""http://index." + RootDatabase.Name + "." +
                        trigger.View.Schema.Name + "." + trigger.View.Name + 
                        ".trigger." + trigger.Name + @".html"">" + ScrubHtml(trigger.Name) + @"</a>";
                    href += @"</td>";
                    sb.Append(href);

                    href = @"<td>";
                    if (Business.FilterPreferences.ShowViewsPage)
                    {
                       href += @"<a href=""http://index." + RootDatabase.Name + "." +
                           trigger.View.Schema.Name + ".table." + trigger.View.Name +
                           @".html"">" + ScrubHtml(trigger.View.Schema.Name) + "." + ScrubHtml(trigger.View.Name) + @"</a>";
                    }
                    else
                    {
                       href += ScrubHtml(trigger.View.Schema.Name) + "." + ScrubHtml(trigger.View.Name);
                    }
                    href += @"</td>";
                    sb.Append(href);

                    if (trigger.Description == "")
                        sb.Append(@"<td>&nbsp;</td></tr>");
                    else
                        sb.Append(@"<td>" + ScrubHtml(trigger.Description) + @"</td></tr>");

                    sb.Append(@"</tr>");
                    AddBodyLine(sb.ToString());
                }

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion

                AddBodyLine(@"</div>");
                _html = CreateHtmlDocument();
            }
            return _html;
        }
        #endregion
        #region Private
        private SqlServer.Database _database;
        #endregion
    }
}
